
import java.net.URL;

import com.vmware.vim25.ClusterConfigSpecEx;
import com.vmware.vim25.HostConnectSpec;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Datacenter;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.Task;


public class VcenterMo {
	
	public static void main(String[] args) throws Exception{
		
		String vCenterUserName ="root";
		
		String vCenterPassword = "vmware";
		
		String vCenterAddress = "10.16.2.59";
		 
		ServiceInstance si = new ServiceInstance(new URL("https://"+vCenterAddress+"/sdk"), vCenterUserName, vCenterPassword, true);
		//查询名称为Test-datacenter 的数据中心
		String datacenterName = "Test-datacenter";
		Datacenter dcObj = (Datacenter) new InventoryNavigator(si.getRootFolder()).searchManagedEntity("Datacenter", datacenterName);
		//查询所有数据中心
		ManagedEntity []dcObjs =  new InventoryNavigator(si.getRootFolder()).searchManagedEntities("Datacenter");
		
		//在datacenter下查询对应名称为Test-cluster的集群
		String clusterName ="Test-cluster";
		
		ClusterComputeResource clusterObj =(ClusterComputeResource) new InventoryNavigator(dcObj).searchManagedEntity("ClusterComputeResource", clusterName );
		 
		System.out.println(clusterObj.getHosts()[0].getHostNetworkSystem().getNetworkConfig().ipV6Enabled);
		
		String hostname ="172.16.201.11";
		
		//查询单个物理机器
		HostSystem host =(HostSystem) new InventoryNavigator(clusterObj).searchManagedEntity("HostSystem", hostname );

		
	}
	
	public Datacenter createdatacenter() throws Exception{
		
		String vCenterAddress = "172.16.211.31";
		String vCenterUserName = "administrator";
		String vCenterPassword = "P@ssw0rd!";
		//建立到vCenter 的链接
		ServiceInstance si = new ServiceInstance(new URL("https://"
		                + vCenterAddress + "/sdk"), vCenterUserName, vCenterPassword,
		                true);
		String datacenterName = "Test-Datacenter";
		//创建数据中心
		Datacenter dc = si.getRootFolder().createDatacenter(datacenterName);
		
		return dc;
	}
	//在数据中心中添加集群
	public void createCluster() throws Exception{
		
		String clusterName = "Test-Cluster";
		//在新创建的数据中心下创建集群
		ClusterConfigSpecEx clusterSpec = new ClusterConfigSpecEx();
		
		Datacenter dc =createdatacenter();
		
		ClusterComputeResource cluster = dc.getHostFolder().createClusterEx(clusterName, clusterSpec );
		
		////在数据中心中添加ESXi主机
		HostConnectSpec hostSpec = new HostConnectSpec();
        String userName = "root";
        String password = "password";
        String host ="172.16.210.111";
        hostSpec.setUserName(userName);
        hostSpec.setPassword(password);
        hostSpec.setHostName(host);
        hostSpec.setForce(true); // forcely take over the host
        //添加主机到集群中
        Task morTask = cluster.addHost_Task(hostSpec, true, null, null);
        String taskResult = morTask.waitForTask();
 
        if (!Task.SUCCESS.equalsIgnoreCase(taskResult)) {
            throw new RuntimeException("Unable to add host " + host
                    + " to vSphere cluster due to " + taskResult);
        }
	}
	

}
